# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Michael Mason <ms13sp@gmail.com>
# Contributor: Leonardo Arena <rnalrd@gmail.com>
# Contributor: Yann Vigara <yvigara@angarium.co.uk>
# Maintainer: Nathan Angelacos <nangel@alpinelinux.org>

pkgname=kamailio
pkgver=6.0.4
pkgrel=0

# If building from a git snapshot, specify the gitcommit
# If building a proper release, leave gitcommit blank or commented
_gitcommit=9bba10f1684f078be9cbedbd1da05743bb3bf513

[ -n "$_gitcommit" ] && pkgver="${pkgver}_git20251011"
[ -n "$_gitcommit" ] && _suffix="-${_gitcommit:0:7}"
[ -n "$_gitcommit" ] && builddir="$srcdir/$pkgname-$_gitcommit" || builddir="$srcdir/$pkgname-$pkgver"
[ -z "$_gitcommit" ] && _gitcommit="$pkgver"

pkgdesc="Open Source SIP Server"
url="https://www.kamailio.org/"
pkgusers="kamailio"
pkggroups="kamailio"
arch="all"
license="GPL-2.0-or-later"
options="!check" # no testsuite
depends="gawk"
makedepends="\
	bison
	cmake
	confuse-dev
	curl-dev
	expat-dev
	flex
	freeradius-client-dev
	hiredis-dev
	hiredis-ssl
	icu-dev
	jansson-dev
	json-c-dev
	libev-dev
	libevent-dev
	libgcrypt-dev
	libmaxminddb-dev
	libmemcached-dev
	libmnl-dev
	libpq-dev
	libunistring-dev
	libuuid
	libwebsockets-dev
	libxml2-dev
	libxslt
	linux-headers
	lksctp-tools-dev
	lua-dev
	mariadb-dev
	mosquitto-dev
	ncurses-dev
	net-snmp-dev
	openldap-dev
	openssl-dev>3
	openssl-libs-static
	pcre2-dev
	perl-dev
	python3-dev
	rabbitmq-c-dev
	rabbitmq-c-static
	readline-dev
	ruby-dev
	sqlite-dev
	unixodbc-dev
	zlib-static
	"

source="kamailio-${pkgver}$_suffix.tar.gz::https://github.com/kamailio/kamailio/archive/$_gitcommit.tar.gz
	kamailio.initd
	"

install="$pkgname.pre-install $pkgname.pre-upgrade"

[ "$CARCH" = "armv7" ] && options="$options textrels"

# secfixes:
#   5.1.4-r0:
#     - CVE-2018-14767

# See Makefile.groups for the list of recommended modules for
# each package.  What follows is a simplified version of Makefile.groups
#
# Alpine specific changes:
#
# _mod_list_debugger is not defined in the original
#
# Move avp from _mod_list_extras to _mod_list_basic, so that we can create
# a kamailio-extras package
#
# Remove db_text from _mod_list_db because we create a separate dbtext package
#
# - basic used modules, with no extra dependency (widespread usage)
_mod_list_basic="\
	async
	auth
	avp
	benchmark
	blst
	cfg_rpc
	cfgt
	cfgutils
	corex
	counters
	ctl
	debugger
	diversion
	dlgs
	enum
	exec
	gzcompress
	ipops
	keepalive
	kemix
	kex
	mangler
	math
	maxfwd
	mediaproxy
	mqueue
	nathelper
	nat_traversal
	path
	pike
	posops
	pv
	pv_headers
	pvtpl
	ratelimit
	rr
	rtimer
	rtpengine
	rtpproxy
	sanity
	sdpops
	siprepo
	sipt
	siputils
	sl
	smsops
	statistics
	statsc
	stun
	sworker
	textops
	textopsx
	tm
	tmx
	topoh
	topos
	xlog
	"

# - extra used modules, with no extra dependency
_mod_list_extras="\
	auth_diameter
	auth_xkeys
	call_control
	call_obj
	cnxcc
	dmq
	dmq_usrloc
	domainpolicy
	file_out
	influxdbc
	log_custom
	msrp
	nosip
	pdb
	pvtpl
	qos
	rtjson
	sca
	seas
	sms
	ss7ops
	sst
	statsd
	tcpops
	timer
	tmrec
	tsilo
	uac_redirect
	xhttp
	xhttp_rpc
	xprint
	"

# - common modules depending on database
_mod_list_db="\
	acc
	alias_db
	auth_db
	avpops
	cfg_db
	db_cluster
	db_flatstore
	dialog
	dispatcher
	domain
	drouting
	group
	htable
	imc
	matrix
	mohqueue
	msilo
	mtree
	pdt
	permissions
	pipelimit
	prefix_route
	p_usrloc
	registrar
	sipcapture
	siptrace
	speeddial
	sqlops
	topos_htable
	uac
	uri_db
	userblocklist
	usrloc
	"

# - common modules depending on database, using UID db schema
_mod_list_dbuid="\
	db2_ops
	uid_auth_db
	uid_avp_db
	uid_domain
	uid_gflags
	uid_uri_db
	"

# - modules depending on pcre3 library
_mod_list_pcre="dialplan lcr regex"

# - modules depending on radius client library
_mod_list_radius="acc_radius auth_radius misc_radius peering"

# - modules depending on ldap client library
_mod_list_ldap="db2_ldap h350 ldap"

# - modules depending on mysql client library
_mod_list_mysql="db_mysql"

# - modules depending on postgres client library
_mod_list_postgres="db_postgres"

# - modules depending on unixodbc library
_mod_list_unixodbc="db_unixodbc"

# - modules depending on mysql cassandra library
_mod_list_cassandra="db_cassandra ndb_cassandra"

# - modules depending on xml2 library
_mod_list_cpl="cplc"

# - modules depending on xml2 library
_mod_list_xml="xhttp_pi xmlrpc xmlops"

# - modules depending on net-snmp library
_mod_list_snmpstats="snmpstats"

# - modules depending on expat library
_mod_list_xmpp="xmpp"

# - modules depending on confuse library
_mod_list_carrierroute="carrierroute"

# - modules depending on curl library
_mod_list_utils="utils http_client"

# - async http_async_client
_mod_list_http_async="http_async_client"

# - modules depending on memcache library
_mod_list_memcached="memcached"

# - modules depending on openssl library
_mod_list_tls="crypto tls"

# - modules depending on static openssl library
_mod_list_tlsa="tlsa"

# - modules depending on openssl library
_mod_list_outbound="outbound"

# - modules depending on unistring library
_mod_list_websocket="lwsc websocket"

# - modules depending on libsctp
_mod_list_sctp="sctp"

# - modules depending on openssl library
_mod_list_authephemeral="auth_ephemeral"

# - modules related to SIMPLE presence extensions
_mod_list_presence="\
	presence
	presence_conference
	presence_dfks
	presence_dialoginfo
	presence_mwi
	presence_profile
	presence_reginfo
	presence_xml
	pua
	pua_bla
	pua_dialoginfo
	pua_reginfo
	pua_rpc
	pua_usrloc
	pua_xmpp
	rls
	xcap_client
	xcap_server
	"

# - modules depending on lua library
_mod_list_lua="app_lua"

# - modules depending on perl library
_mod_list_perl="app_perl db_perlvdb"

# - modules depending on python3 library
_mod_list_python3="app_python3 app_python3s"

# - modules depending on ruby library
_mod_list_ruby="app_ruby app_ruby_proc"

# - modules depending on rabbitmq
_mod_list_rabbitmq="rabbitmq"

# - modules depending on mqtt mosquitto and ev library
_mod_list_mqtt="mqtt"

# - modules depending on geoip2 library
_mod_list_geoip2="geoip2"

# - modules depending on sqlite library
_mod_list_sqlite="db_sqlite"

# - modules depending on oracle library
_mod_list_oracle="db_oracle"

# - modules depending on json library
_mod_list_json="json pua_json jsonrpcc jsonrpcs"

# - modules depending on redis library
_mod_list_redis="db_redis ndb_redis topos_redis"

# - modules depending on mono library
_mod_list_mono="app_mono"

# - modules related to IMS extensions
_mod_list_ims="\
	cdp
	cdp_avp
	ims_auth
	ims_charging
	ims_dialog
	ims_diameter_server
	ims_icscf
	ims_ipsec_pcscf
	ims_isc
	ims_ocs
	ims_qos
	ims_qos_npn
	ims_registrar_pcscf
	ims_registrar_scscf
	ims_usrloc_pcscf
	ims_usrloc_scscf
	"

# - modules depending on osp toolkit library
_mod_list_osp="osp"

# - modules depending on java library
_mod_list_java="app_java"

# - modules depending on gzip library
_mod_list_gzcompress="gzcompress"

# - modules depending on uuid library
_mod_list_uuid="uuid"

# - modules depending on ev library
_mod_list_ev="evapi"

# - modules depending on kazoo/rabbitmq
_mod_list_kazoo="kazoo"

# - modules depending on mongodb
_mod_list_mongodb="db_mongodb ndb_mongodb"

# - modules depending on jansson library
_mod_list_jansson="jansson janssonrpcc acc_json"

# - modules depending on libm
_mod_list_jsdt="app_jsdt"

# - modules depending on rabbitmq
_mod_list_rabbitmq="rabbitmq"

# - modules depending on sipdump
_mod_list_sipdump="sipdump"

# - modules depending on libgcrypt
_mod_list_gcrypt="gcrypt"

# - modules depending on libmicrohttpd
_mod_list_microhttpd="microhttpd"

# - modules depending on nghttp2 library
_mod_list_nghttp2=nghttp2

# - kemix module
_mod_list_kemix="kemix"

# - LOST
_mod_list_lost="lost"

# - modules depending on xhttp_prom
_mod_list_xhttp_prom="xhttp_prom"

# - modules depending on slack
_mod_list_slack="slack"

# - modules depending on lrkproxy
_mod_list_lrkproxy="lrkproxy"

# Alpine Specific
_mod_list_debugger="benchmark debugger misctest"

# Standard modules in main pkg
_module_group_kstandard="$_mod_list_basic $_mod_list_dbuid $_mod_list_pcre"

# END Makefile.groups #############################################################

subpackages="\
	authephemeral
	carrierroute
	cpl
	db
	dbtext
	debugger
	ev
	extras
	gcrypt
	geoip2
	http_async
	ims
	jansson
	jsdt
	json
	kazoo
	kemix
	ldap
	lost
	lrkproxy
	lua
	memcached
	mqtt
	mysql
	nghttp2
	outbound
	perl
	postgres
	presence
	python3
	rabbitmq
	radius
	redis
	ruby
	sctp
	sipdump
	slack
	snmpstats
	sqlite
	tls
	tlsa
	unixodbc
	utils
	uuid
	websocket
	xhttp_prom
	xml
	xmpp
	"

. ${startdir}/APKBUILD-kamailio

_modules="$_module_group_kstandard"
for _i in $subpackages; do
	eval "_modules=\"\$_modules \$_mod_list_$_i\""
done
# format modules as string
_modules=$(echo $_modules)

subpackages="doc dbg openrc $subpackages"
# add '$pkgname-' prefix
subpackages=$(echo $subpackages | sed -E "s/\w+/$pkgname-&/g")

snapshot() {
	if [ ! -f "$SRCDEST"/$pkgname-${pkgver}$_suffix.tar.gz ]; then
		wget -O "$SRCDEST"/$pkgname-${pkgver}$_suffix.tar.gz https://github.com/kamailio/$pkgname/archive/$_gitcommit.tar.gz
	fi
	SHA512SUM=$(sha512sum "$SRCDEST"/$pkgname-${pkgver}$_suffix.tar.gz | sed -e "s:$SRCDEST/::")
	sed -i -e "s/^\(sha512sums=.\).*/\1$SHA512SUM/" APKBUILD
}

build() {
	export CMAKE_C_FLAGS_MINSIZEREL="-g"
	# for linux/386 arch maybe usefull to set CMAKE_TOOLCHAIN_FILE
	# use TOOLCHAIN_OPTIONS environment variable if defined
	# more details at https://github.com/kamailio/kamailio/issues/4403
	cmake -S . ${TOOLCHAIN_OPTIONS} \
		-G "Unix Makefiles" \
		-B build \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DVERBOSE=ON \
		-DBUILD_DOC=ON \
		-DRADIUSCLIENT="FREERADIUS" \
		--fresh \
		-DCMAKE_BUILD_TYPE="MinSizeRel" \
		-DINCLUDE_MODULES="$_modules"
	cmake --build build
	cmake --build build --target man
	cmake --build build --target dbschema
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	install -m755 -D "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -d -o kamailio "$pkgdir"/run/kamailio
}

_mv_mod() {
	local moddir=usr/lib/kamailio/modules i=
	mkdir -p "$subpkgdir"/$moddir
	for i in "$@"; do
		mv "$pkgdir"/$moddir/$i.so "$subpkgdir"/$moddir/
	done
}

_generic_pkg() {
	pkgdesc="Kamailio $1"
	depends="$pkgname"
	_mv_mod $2
}

_db_driver() {
	pkgdesc="Database drivers for $1"
	depends="$pkgname $pkgname-db"
	_mv_mod db_$2
	mkdir -p "$subpkgdir"/usr/share/kamailio \
		"$subpkgdir"/usr/lib/kamailio/kamctl
	mv "$pkgdir"/usr/share/kamailio/$3 \
		"$subpkgdir"/usr/share/kamailio/
	mv "$pkgdir"/usr/lib/kamailio/kamctl/*.$4 \
		"$subpkgdir"/usr/lib/kamailio/kamctl/
}

db() {
	_generic_pkg "modules using a database backend" "$_mod_list_db"
}

postgres() {
	_db_driver Postgres postgres postgres pgsql
}

mysql() {
	_db_driver Mysql mysql mysql mysql
}

sqlite() {
	_db_driver SQlite sqlite db_sqlite sqlite
}

dbtext() {
	_db_driver DBText text dbtext dbtext
	# kamdbctl.dbtext is a python script - but we will not make python
	# a dependency because kamdbctl is not normally used
	mkdir -p "$subpkgdir"/usr/lib/kamailio/kamctl
	mv "$pkgdir"/usr/lib/kamailio/kamctl/dbtextdb \
		"$subpkgdir"/usr/lib/kamailio/kamctl/
}

cpl() {
	_generic_pkg "CPL (Call Processing Language) interpreter" \
		"$_mod_list_cpl"
}

xml() {
	_generic_pkg "XML related modules: $_mod_list_xml" \
		"$_mod_list_xml"
	mkdir -p "$subpkgdir"/usr/share/kamailio
	mv "$pkgdir"/usr/share/kamailio/xhttp_pi \
		"$subpkgdir"/usr/share/kamailio
	mkdir -p "$subpkgdir"/etc/kamailio/
	mv "$pkgdir"/etc/kamailio/pi_framework.xml \
		"$subpkgdir"/etc/kamailio
}

unixodbc() {
	_generic_pkg "Database drivers for unixodbc" \
		"$_mod_list_unixodbc"
}

snmpstats() {
	_generic_pkg "SNMP statistics support" \
		"$_mod_list_snmpstats"
	mkdir -p "$subpkgdir"/usr/share/snmp
	mv "$builddir"/src/modules/snmpstats/mibs \
		"$subpkgdir"/usr/share/snmp/
}

xmpp() {
	_generic_pkg "XMPP (Jabber) gateway" \
		"$_mod_list_xmpp"
}

carrierroute() {
	_generic_pkg "carrier grade routing functions" \
		"$_mod_list_carrierroute"
}

ldap() {
	_generic_pkg "LDAP search functions" \
		"$_mod_list_ldap"
}

utils() {
	_generic_pkg "miscellaneous utility functions" \
		"$_mod_list_utils"
}

tls() {
	_generic_pkg "TLS support" \
		"$_mod_list_tls"
}

tlsa() {
	_generic_pkg "TLS support compiled statically against libssl and libcrypto" \
		"$_mod_list_tlsa"
}

presence() {
	_generic_pkg "SIP Notify (Presence) support" \
		"$_mod_list_presence"
}

lua() {
	_generic_pkg "Lua script support" \
		"$_mod_list_lua"
}

ims() {
	_generic_pkg "IP Multimedia Subsystem (IMS) support" \
		"$_mod_list_ims"
}

outbound() {
	_generic_pkg "RFC 5626 section 5 Outbound support" \
		"$_mod_list_outbound"
}

debugger() {
	_generic_pkg "debugging modules" \
		"$_mod_list_debugger"
}

extras() {
	_generic_pkg "extra modules" \
		"$_mod_list_extras"
}

websocket() {
	_generic_pkg "websocket support" \
		"$_mod_list_websocket"
}

authephemeral() {
	_generic_pkg "auth ephemeral (webrtc)" \
		"$_mod_list_authephemeral"
}

json() {
	_generic_pkg "Operations on json strings" \
		"$_mod_list_json"
}

jansson() {
	_generic_pkg "Operations on json strings using jansson library" \
		"$_mod_list_jansson"
}

geoip2() {
	_generic_pkg "MaxMind GeoIP lookup support" \
		"$_mod_list_geoip2"
}

ev() {
	_generic_pkg "Network event broadcast API" \
		"$_mod_list_ev"
}

uuid() {
	_generic_pkg "UUID generator using libuuid" \
		"$_mod_list_uuid"
}

memcached() {
	_generic_pkg "memcached support" \
		"$_mod_list_memcached"
}

rabbitmq() {
	_generic_pkg "RabbitMQ client for configuration file" \
		"$_mod_list_rabbitmq"
}

jsdt() {
	_generic_pkg "Support for executing JavaScript" \
		"$_mod_list_jsdt"
}

redis() {
	_generic_pkg "Redis NoSQL support" \
		"$_mod_list_redis"
}

http_async() {
	_generic_pkg "Asynchronous HTTP queries support" \
		"$_mod_list_http_async"
}

kazoo() {
	_generic_pkg "Kazoo VoIP platform support" \
		"$_mod_list_kazoo"
}

radius() {
	_generic_pkg "RADIUS modules for Kamailio" \
		"$_mod_list_radius"
}

sctp() {
	_generic_pkg "SCTP transport for Kamailio" \
		"$_mod_list_sctp"
}

perl() {
	_generic_pkg "Perl extensions and database driver for Kamailio" \
		"$_mod_list_perl"
}

python3() {
	_generic_pkg "Execute embedded Python3 scripts" \
		"$_mod_list_python3"
}

ruby() {
	_generic_pkg "Execute embedded Ruby scripts" \
		"$_mod_list_ruby"
}

mqtt() {
		_generic_pkg "MQTT publish/subscribe connector" \
		"$_mod_list_mqtt"
}

rabbitmq() {
	_generic_pkg "RabbitMQ related modules for Kamailio" \
		"$_mod_list_rabbitmq"
}

sipdump() {
	_generic_pkg "This module writes SIP traffic and some associated details into local files" \
		"$_mod_list_sipdump"
}

microhttpd() {
	_generic_pkg "Simple HTTP server using libmicrohttpd" \
		"$_mod_list_microhttpd"
}

kemix() {
	_generic_pkg "extensions and functions to be used with KEMI SIP routing scripting" \
		"$_mod_list_kemix"
}

lost() {
	_generic_pkg "HELD (RFC6155) and LOST (RFC5222) location-based routing" \
		"$_mod_list_lost"
}

xhttp_prom() {
	_generic_pkg "Metrics for Prometheus monitoring." \
		"$_mod_list_xhttp_prom"
}

slack() {
	_generic_pkg "Send messages to Slack channels" \
		"$_mod_list_slack"
}

lrkproxy() {
	_generic_pkg "RTP packets relay with kernel based forwarding" \
		"$_mod_list_lrkproxy"
}

nghttp2() {
	_generic_pkg "HTTP2 server implementation using nghttp2 library" \
		"$_mod_list_nghttp2"
}

gcrypt() {
	_generic_pkg "Cryptography functions using libcrypt" \
		"$_mod_list_gcrypt"
}

sha512sums="
32b7dfa502d9e6d15c3edc51bd42cd99dd9793445c5a4cf56acbf13f97e7c2647628ad96e22f5c1f573f3b3e8aba933ccb6ac85c389cdf7cb402adc5067c9776  kamailio-6.0.4_git20251011-9bba10f.tar.gz
38a786b48d433b2e99a29249458c1ffcc3f449a1898df6ef1f4b8ebf7770e99d80ce6e3340ea680b44e725b951e65fd06d395ecb903600d7c8fdc0a736c4d9b1  kamailio.initd
"
